package com.example.lovedogs.fragment;




import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.example.lovedogs.R;
import com.example.lovedogs.zizhen.SickInfoStateActivity;
import com.example.lovedogs.zizhen.SickInfofastActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class ZizhenFragment extends Fragment{
	

	
	private final class VpAdapter extends PagerAdapter {
		private List<View> views = null;

		public VpAdapter(List<View> views) {
			this.views = views;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(views.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(views.get(arg1), 0);
			return views.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}
	private final class GuidePageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {
			what.getAndSet(arg0);
			for (int i = 0; i < imageViews.length; i++) {
				imageViews[arg0]
						.setBackgroundResource(R.drawable.banner_dian_focus);
				if (arg0 != i) {
					imageViews[i]
							.setBackgroundResource(R.drawable.banner_dian_blur);
				}
			}
		}
	}
	private final Handler viewHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			vp_attention.setCurrentItem(msg.what);
			super.handleMessage(msg);
		}
	};
	private class MyAdapter_listview1 extends BaseAdapter{
	
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return images.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		 /**
		 * 有多少数据这个方法就调用多少次
		 * postion:每个条目的位置
		 * convertView:每个条目的缓存View对象
		 * parent:ListView对象
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = inflater.inflate(R.layout.listitem_zizhen_listview1, null);
			}
			ImageView imageView_item=(ImageView) convertView.findViewById(R.id.imageView_item);
			TextView tv_title = (TextView) convertView.findViewById(R.id.tv_title);
			TextView tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
		
			imageView_item.setImageResource(images[position]);
			tv_title.setText(title[position]);
			tv_desc.setText(desc[position]);
			
			return convertView;
		}
		
	}
	
	private class MyOnItemClickListener_lv1 implements OnItemClickListener{

		Intent intent;
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (position) {
			case 0:
				intent =new Intent(currentActivity,SickInfofastActivity.class);
				currentActivity.startActivity(intent);
				break;
			case 1:
				intent =new Intent(currentActivity,SickInfoStateActivity.class);
				intent.putExtra("info", "state");
				currentActivity.startActivity(intent);
				break;

			default:
				intent =new Intent(currentActivity,SickInfoStateActivity.class);
				intent.putExtra("info", "body");
				currentActivity.startActivity(intent);
				break;
			}
			
		}
		
	}
	private Activity currentActivity;
	private View rootView;//当前view对象  用于findViewById();
	private ScrollView sv_zizhen;
	private ListView listview1;
	private ViewPager vp_attention;
	private ListView lv_recommenddoctor;
	//listview
	private int[] images={R.drawable.img00,R.drawable.img01,R.drawable.img02};
	private String[] title={"快速通道","状态","躯体"};
	private String[] desc={"常见症状、快熟查询","精神状态、运动举止等","躯体状态、体重变化等"};
	private LayoutInflater inflater;
	//viewpager
	private ImageView[] imageViews = null;
	private AtomicInteger what = new AtomicInteger(0);
	private boolean isContinue = true;
	
	int image[]={
			R.drawable.img00,
			R.drawable.img00,
			R.drawable.img00,
			R.drawable.img00,
			R.drawable.img00,
			R.drawable.img00,
			R.drawable.img00,
			R.drawable.img00,
			R.drawable.img00
	};

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.inflater=inflater;
		rootView = inflater.inflate(R.layout.fragment_zizhen, container, false);
		currentActivity=this.getActivity();
		
		sv_zizhen = (ScrollView) rootView.findViewById(R.id.sv_zizhen);
		sv_zizhen.smoothScrollTo(0, 0);
		
    	listview1=(ListView) rootView.findViewById(R.id.listView1);
		MyAdapter_listview1 adapter_list1= new MyAdapter_listview1();
		listview1.setAdapter(adapter_list1);
		listview1.setOnItemClickListener(new MyOnItemClickListener_lv1());
		
		
		lv_recommenddoctor=(ListView) rootView.findViewById(R.id.lv_recommenddoctor);
		
		initViewPager();
		
		
		
		List<News> data = new ArrayList<News>();
		data.add(new News("新闻标题1","qwwww"));
		data.add(new News("新闻标题2","qeeeee"));
		data.add(new News("新闻标题3","333"));
		data.add(new News("新闻标题4","222222222"));
		data.add(new News("新闻标题5","wwwwwwwww"));
		data.add(new News("新闻标题6","wwwwwwwww"));
		data.add(new News("新闻标题7","wwwwwwwwwww"));
		data.add(new News("新闻标题8","wwwwwwww"));
		data.add(new News("新闻标题9","wwwwwwww"));
		MyAdapter_forlist adapter_list=new MyAdapter_forlist(data);
		lv_recommenddoctor.setAdapter(adapter_list);
//		adapter_list.setListViewHeightBasedOnChildren(lv_recommenddoctor);
		
		return rootView;
	}
	
	
	private void initViewPager() {
		vp_attention=(ViewPager) rootView.findViewById(R.id.vp_attention);
		ViewGroup group = (ViewGroup) rootView.findViewById(R.id.viewGroup);
		// 这里存放的是四张广告背景
		List<View> advPics = new ArrayList<View>();
		ImageView img1 = new ImageView(currentActivity);
		img1.setBackgroundResource(R.drawable.image01);
		advPics.add(img1);
		ImageView img2 = new ImageView(currentActivity);
		img2.setBackgroundResource(R.drawable.image02);
		advPics.add(img2);
		ImageView img3 = new ImageView(currentActivity);
		img3.setBackgroundResource(R.drawable.image03);
		advPics.add(img3);
		ImageView img4 = new ImageView(currentActivity);
		img4.setBackgroundResource(R.drawable.image04);
		advPics.add(img4);

		// 对imageviews进行填充
		
		imageViews = new ImageView[advPics.size()];
		// 小图标
		ImageView imageView = null;
		for (int i = 0; i < advPics.size(); i++) {
			imageView = new ImageView(currentActivity);
			imageView.setLayoutParams(new LayoutParams(20, 20));
			imageView.setPadding(5, 5, 5, 5);
			imageViews[i] = imageView;
			if (i == 0) {
				imageViews[i]
						.setBackgroundResource(R.drawable.banner_dian_focus);
			} else {
				imageViews[i]
						.setBackgroundResource(R.drawable.banner_dian_blur);
			}
			group.addView(imageViews[i]);
		}
		vp_attention.setAdapter(new VpAdapter(advPics));
		vp_attention.setOnPageChangeListener(new GuidePageChangeListener());
		vp_attention.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
				case MotionEvent.ACTION_MOVE:
					isContinue = false;
					break;
				case MotionEvent.ACTION_UP:
					isContinue = true;
					break;
				default:
					isContinue = true;
					break;
				}
				return false;
			}
		});
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					if (isContinue) {
						viewHandler.sendEmptyMessage(what.get());
						whatOption();
					}
				}
			}
		}).start();
	}

	
	private void whatOption() {
		what.incrementAndGet();
		if (what.get() > imageViews.length - 1) {
			what.getAndAdd(-4);
		}
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {

		}
	}

	class MyAdapter_forlist extends BaseAdapter{
		private List<News> data;
		public MyAdapter_forlist(List<News> data) {
			this.data=data;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		/**
		 * 有多少数据这个方法就调用多少次
		 * postion:每个条目的位置
		 * convertView:每个条目的缓存View对象
		 * parent:ListView对象
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				convertView = inflater.inflate(R.layout.list_item, null);
			}
			ImageView imageView_list=(ImageView) convertView.findViewById(R.id.imageView_list);
			imageView_list.setImageResource(image[position]);
			TextView tv_title = (TextView) convertView.findViewById(R.id.tv_title);
			TextView tv_desc = (TextView) convertView.findViewById(R.id.tv_desc);
			TextView tv_zan=(TextView) convertView.findViewById(R.id.tv_zan);
			
			
			News news = data.get(position);
			tv_title.setText(news.getTitle());
			tv_desc.setText(news.getDesc());
			System.out.println(news.getI());
			tv_zan.setText(news.getI()+"");
			
			tv_zan.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
//					Toast.makeText(MainActivity.this, "点赞", 0).show();
					
					// 实现点赞加一功能
					
				}
			});
			return convertView;
		}
		
	}
}
